package net;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class HttpsRequest {

	public static String sendGet(String url, String param) {

		HttpsURLConnection.setDefaultHostnameVerifier(new HttpsRequest().new NullHostNameVerifier());
		SSLContext sc;
		try {
			sc = SSLContext.getInstance("TLS");
			sc.init(null, trustAllCerts, new SecureRandom());
			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
			URL httpsUrl = new URL(url + "?" + param);
			// 打开restful链接
			HttpURLConnection conn = (HttpURLConnection) httpsUrl.openConnection();
			conn.setRequestMethod("GET");
			// POST GET PUT DELETE
			// 设置访问提交模式，表单提交
			conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
			conn.setConnectTimeout(130000);
			// 连接超时 单位毫秒
			conn.setReadTimeout(130000);
			// 读取超时 单位毫秒
			// 读取请求返回值
			byte bytes[] ;
			InputStream inStream = conn.getInputStream();
			if(inStream.available()>2048){
				 bytes = new byte[2048];
			}else{
				 bytes = new byte[inStream.available()];
			}
			
			  DataInputStream in = new DataInputStream(inStream);
			  long mLen = in.available();
			  long remainLen=mLen;
			  StringBuffer sb = new  StringBuffer();
              //将文件内容进行从gbk到utf-8的编码格式转化
              while(remainLen>0) {
                  if(remainLen>4096) {
                	  inStream.read(bytes,0,4096);
                      sb.append(new String(bytes, "utf-8"));
                      remainLen -=4096;
                  }
                  else {
                	  inStream.read(bytes,0,(int) remainLen);
                      sb.append(new String(bytes, "utf-8"));
              				remainLen -=remainLen;
                  }
              }
			
		//	inStream.read(bytes, 0, inStream.available());
		//	String result = new String(bytes, "utf-8");
			return sb.toString();

		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (KeyManagementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	// public static void main(String[] args) throws IOException,
	// KeyManagementException, NoSuchAlgorithmException {
	// String param =
	// "grant_type="+HttpConfig.grant_type+"&client_secret="+HttpConfig.client_secret+"&client_id="+HttpConfig.client_id;
	// String a = HttpsRequest.sendGet(HttpConfig.storageServer+"/oauth/token",
	// param);
	// System.out.println(a);
	// }

	static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
		@Override
		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			// TODO Auto-generated method stub
		}

		@Override
		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			// TODO Auto-generated method stub
		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			// TODO Auto-generated method stub
			return null;
		}
	} };

	public class NullHostNameVerifier implements
			HostnameVerifier { /*
								 * (non-Javadoc)
								 * 
								 * @see
								 * javax.net.ssl.HostnameVerifier#verify(java.
								 * lang.String, javax.net.ssl.SSLSession)
								 */
		@Override
		public boolean verify(String arg0, SSLSession arg1) {
			// TODO Auto-generated method stub
			return true;
		}
	}

}
